#include "IntersecList.h"

void SListPrint(ListNode* plist)
{
    ListNode* cur = plist;
    while (cur)
    {
        printf("%d->", cur->val);
        cur = cur->next;
    }
    printf("NULL\n");
}

struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
    int lenA = 1, lenB = 1;
    struct ListNode* curA = headA;
    struct ListNode* curB = headB;
    while (curA != NULL)
    {
        lenA++;
        curA = curA->next;
    }
    while (curB != NULL)
    {
        lenB++;
        curB = curB->next;
    }

    int dif = abs(lenA - lenB);
    struct ListNode* longlist = headA, * shortlist = headB;
    if (lenA < lenB)
    {
        longlist = headB;
        shortlist = headA;
    }

    while (dif--)
    {
        longlist = longlist->next;
    }
    while (longlist != shortlist)
    {
        longlist = longlist->next;
        shortlist = shortlist->next;
    }
    return longlist;
}